package com.itlong.cloud.commons.service.impl;

import com.itlong.cloud.POJO.VO.app.AppCreateBaiduFaceVO;
import com.itlong.cloud.POJO.VO.app.AppUserFaceVO;
import com.itlong.cloud.POJO.VO.operate.OperateFaceApplyInfoVO;
import com.itlong.cloud.POJO.VO.property.VisitorFaceInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.commons.dao.ICommonsDao;
import com.itlong.cloud.commons.service.IVisitorFaceService;
import com.itlong.cloud.face.baidu.BaiduFaceService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.file.UploadFileUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * <desc>
 *      访客人脸实现类
 * </desc>
 *
 * @createDate 2019/03/04
 */
@Service
@RefreshScope
public class IVisitorFaceServiceImpl implements IVisitorFaceService {

    @Autowired
    ICommonsDao iCommonsDao;

    @Autowired
    BaiduFaceService baiduFaceService;

    @Autowired
    RedisService redisService;

    @Value("${" + PlatformConstants.FILE_FTP_HOST + "}")
    public String FILE_FTP_HOST;
    @Value("${" + PlatformConstants.FILE_FTP_PORT + "}")
    public String FILE_FTP_PORT;
    @Value("${" + PlatformConstants.FILE_FTP_USERNAME + "}")
    public String FILE_FTP_USERNAME;
    @Value("${" + PlatformConstants.FILE_FTP_PASSWORD + "}")
    public String FILE_FTP_PASSWORD;
    @Value("${" + PlatformConstants.FILE_ACCESS_URL + "}")
    public String FILE_ACCESS_URL;

    /**
     * <desc>
     *     删除过期访客人脸
     * <desc/>
     *
     *@param
     * @return
     * @author Qiang.S
     * @createDate 2019/03/01
     */
    @Override
    public void visitorFaceDelete() {
        List<VisitorFaceInfoVO> list = iCommonsDao.getExpiredFaceId();
        for (VisitorFaceInfoVO faceInfoVO : list) {
            //判断项目是否开启人脸识别,并查询是否有百度库key
            OperateFaceApplyInfoVO operateFaceApplyInfoVO = iCommonsDao.getFaceByProjectId(faceInfoVO.getProjectId());
            AppCreateBaiduFaceVO appCreateBaiduFaceVO = new AppCreateBaiduFaceVO();
            appCreateBaiduFaceVO.setUid(faceInfoVO.getVFaceId());
            appCreateBaiduFaceVO.setAPP_ID(operateFaceApplyInfoVO.getAppId());
            appCreateBaiduFaceVO.setAPI_KEY(operateFaceApplyInfoVO.getApiKey());
            appCreateBaiduFaceVO.setSECRET_KEY(operateFaceApplyInfoVO.getSecretKey());
            String result = baiduFaceService.deleteUser(appCreateBaiduFaceVO);
            if (result == null) {
                iCommonsDao.updateVisitorFaceFlag(faceInfoVO.getVFaceId());
            }
        }
    }

    public static void main(String[] args) {
        String s = "http://test.img.jia-r.com/baiduFaceCreateFile/19/04/24/19042415082247294K1.jpg";
        s = s.replace("http://test.img.jia-r.com/","");
        String[] s1 = s.split("/");
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<= s1.length-2;i++) {
            sb.append(s1[i]+"/");
        }
        String url = sb.toString();
        url = url.substring(0,url.length()-1);
        System.out.println(url);
        System.out.println(s1[s1.length-1]);


        UploadFileUtil.UploadFile uploadFileUtil = new UploadFileUtil().new UploadFile(
                "localhost",
                "itlongftp",
                "Itlongserver%2017",
                "21"
        );
        System.out.println(UploadFileUtil.deleteFile(uploadFileUtil, url, s1[s1.length-1]));
    }

    /**
     * <desc>
     *      注销用户删除人脸
     * </desc>
     *
     * @param userId
     * @author Jiaqi.X
     * @createDate 2020/06/17s
     */
    public void deleteUserFace(String userId){
        List<String> projects = iCommonsDao.getProjectsByUserId(userId);
        projects.forEach(project-> {
            AppUserFaceVO vo = iCommonsDao.getCreateBaiduFaceInfo(userId, project);
            if (vo != null && StringUtils.isNotBlank(vo.getFacePicUrl())) {
                String facePicUrl = vo.getFacePicUrl().replace(FILE_ACCESS_URL,"");
                String[] faceUrlPart = facePicUrl.split("/");
                StringBuilder sb = new StringBuilder();
                UploadFileUtil.UploadFile uploadFileUtil = new UploadFileUtil().new UploadFile(
                        FILE_FTP_HOST,
                        FILE_FTP_USERNAME,
                        FILE_FTP_PASSWORD,
                        FILE_FTP_PORT
                );
                for (int i = 0; i <= faceUrlPart.length - 2; i++) {
                    sb.append(faceUrlPart[i] + "/");
                }
                String url = sb.toString();
                url = url.substring(0, url.length() - 1);
                try {
                    //删除ftp上的图片
                    UploadFileUtil.deleteFile(uploadFileUtil, url, faceUrlPart[faceUrlPart.length - 1]);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            OperateFaceApplyInfoVO operateFaceApplyInfoVO = iCommonsDao.getFaceByProjectId(project);
            if (operateFaceApplyInfoVO != null) {
                AppCreateBaiduFaceVO appCreateBaiduFaceVO = new AppCreateBaiduFaceVO();
                appCreateBaiduFaceVO.setUid(userId);
                appCreateBaiduFaceVO.setAPP_ID(operateFaceApplyInfoVO.getAppId());
                appCreateBaiduFaceVO.setAPI_KEY(operateFaceApplyInfoVO.getApiKey());
                appCreateBaiduFaceVO.setSECRET_KEY(operateFaceApplyInfoVO.getSecretKey());
                String result = baiduFaceService.deleteUser(appCreateBaiduFaceVO);
            }
        });
    }

    /**
     * <desc>
     *      京东非标-注销用户删除人脸(仅删除该用户的京东项目下的人脸，区别于标准人脸SDK接口)
     * </desc>
     *
     * @param userId 用户id
     * @Author Zhu.yj
     * @createDate  2020/12/23
     */
    @Override
    public void deleteJdUserFace(String userId, String projectId) {
        if (StringUtils.isBlank(projectId)){
            return;
        }
        AppUserFaceVO vo = iCommonsDao.getCreateBaiduFaceInfo(userId, projectId);
        if (vo != null && StringUtils.isNotBlank(vo.getFacePicUrl())) {
            String facePicUrl = vo.getFacePicUrl().replace(FILE_ACCESS_URL,"");
            String[] faceUrlPart = facePicUrl.split("/");
            StringBuilder sb = new StringBuilder();
            UploadFileUtil.UploadFile uploadFileUtil = new UploadFileUtil().new UploadFile(
                    FILE_FTP_HOST,
                    FILE_FTP_USERNAME,
                    FILE_FTP_PASSWORD,
                    FILE_FTP_PORT
            );
            for (int i = 0; i <= faceUrlPart.length - 2; i++) {
                sb.append(faceUrlPart[i] + "/");
            }
            String url = sb.toString();
            url = url.substring(0, url.length() - 1);
            try {
                //删除ftp上的图片
                UploadFileUtil.deleteFile(uploadFileUtil, url, faceUrlPart[faceUrlPart.length - 1]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        OperateFaceApplyInfoVO operateFaceApplyInfoVO = iCommonsDao.getFaceByProjectId(projectId);
        if (operateFaceApplyInfoVO != null) {
            AppCreateBaiduFaceVO appCreateBaiduFaceVO = new AppCreateBaiduFaceVO();
            appCreateBaiduFaceVO.setUid(userId);
            appCreateBaiduFaceVO.setAPP_ID(operateFaceApplyInfoVO.getAppId());
            appCreateBaiduFaceVO.setAPI_KEY(operateFaceApplyInfoVO.getApiKey());
            appCreateBaiduFaceVO.setSECRET_KEY(operateFaceApplyInfoVO.getSecretKey());
            String result = baiduFaceService.deleteUser(appCreateBaiduFaceVO);
        }
    }
}
